#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<pthread.h>
#include<unistd.h>
using namespace std;

// void* handlerTask(void* threadname)
// {
//     while(true)
//     {
//         sleep(1);
//         cout<<"I am "<<string((char*)threadname)<<endl;
//     }
//     delete[] threadname;
//     return nullptr;
// }

class Result
{
public:
    Result(int result):_result(result){}
    ~Result(){}
    int getResult(){return _result;}
    void Print(){
        cout<<"result"<<" : "<<_result<<endl;
    }
private:
    int _result;
};
class Task
{
public:
    Task(int x,int y):_x(x),_y(y){}
    int Execute()
    {
        return _x+_y;
    }
    ~Task(){}
private:
    int _x;
    int _y;
};
class ThreadTask
{
public:
    ThreadTask(int x,int y,string threadname):_t(x,y),_threadname(threadname){}
    int Run()
    {
        return _t.Execute();
    }
    string threadname()
    {
        return _threadname;
    }
    ~ThreadTask(){}
private:
    Task _t;
    string _threadname;
};
void* handlerTask(void* tasks)
{
    ThreadTask* task = (ThreadTask*)tasks;
    //cout<<"I am "<<task->threadname()<<endl;

    int result = task->Run();
    delete task;
    Result* res = new Result(result);
    return res;
}

// 创建多个线程
int main()
{
    vector<pthread_t> threads;
    for(int i =0;i<5;i++)
    {
        char* threadName = new char[64];
        snprintf(threadName,64,"thread-%d",i+1);
        ThreadTask* tasks = new ThreadTask(10,50,string(threadName));
        pthread_t tid;
        pthread_create(&tid,nullptr,handlerTask,tasks);
        // 创建线程之后,将tid保存起来
        threads.push_back(tid);
    }
    void* res = nullptr;
    vector<Result*> results;
    for(auto tid:threads)
    {
        pthread_join(tid,&res);
        results.push_back((Result*)res);
    }
    for(auto e: results){
        e->Print();
    }
    return 0;
}